package com.example.demo.controller;

import bom.example.common.common.Results;
import bom.example.common.excel.utils.EasyExcelUtil;
import bom.example.common.service.CacheService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.domain.User;
import com.example.demo.domain.model.UserModel;
import com.example.demo.domain.vo.UserVo;
import com.example.demo.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * @author X.I.O
 * @title: UserController
 * @projectName springboot
 * @description: TODO
 * @date 2021/10/26 10:23
 */
@RestController
@Api(tags = {"测试demo"})
@RequestMapping("user")
public class UserController_1 {

    @Autowired
    private UserService userService;

    @ApiOperation("获取用户列表")
    @GetMapping("getUser")
    @PreAuthorize("hasRole('ADMIN')")
    public JSONObject getUserPageList(@RequestParam(value = "page",defaultValue = "1")Integer page,
                                      @RequestParam(value = "size",defaultValue = "10")Integer size,
                                      @RequestParam(value = "keyword",required = false)String keyword){
        LambdaQueryWrapper<User> lambdaQueryWrapper=new LambdaQueryWrapper();
        if(StringUtils.isNotBlank(keyword)){
            lambdaQueryWrapper.or().like(User::getPhone,keyword);
            lambdaQueryWrapper.or().like(User::getRealname,keyword);
            lambdaQueryWrapper.or().like(User::getCreatetime,keyword);
        }
        lambdaQueryWrapper.orderByDesc(User::getCreatetime);
        Page pageModel=new Page(page,size);
        IPage<User> page1=userService.page(pageModel,lambdaQueryWrapper);
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("code",0);
        jsonObject.put("data",page1);
        return jsonObject;
    }

    @ApiOperation("user集合")
    @PostMapping("/userlist")
    public JSONObject getUserListController(){
        List<User> users=userService.list();
        JSONObject object=new JSONObject();
        object.put("data",users);
        return object;
    }

    @ApiOperation("分页sql测试")
    @PostMapping("usertest")
    public JSONObject getUserListPageController(@ApiParam("页码") @RequestParam(value = "page",defaultValue = "1") Integer page,
                                                @ApiParam("条数") @RequestParam(value = "size",defaultValue = "10") Integer size,
                                                @ApiParam("关键字") @RequestParam(value = "keyword",required = false) String keyword){
        List<User> users=userService.selectUserPageList(keyword,page,size);
        return Results.success(users,10);
    }

    /**
     * 请求体 {"username":"admin1","password":"123456","realname":"里斯","phone":"15874587458","status":"1"}
     * @param userVo
     * @return
     */
    @ApiOperation("user-demo新增")
    @PostMapping("addusertest")
    public JSONObject addUserController(@RequestBody @Validated UserVo userVo){
        User user=new User();
        BeanUtils.copyProperties(userVo,user);
        userService.save(user);
        return Results.success();
    }

    /**
     * 请求体：["1453563602672852993","1453563613150224385"]
     * @param ids
     * @return
     */
    @ApiOperation("user根据id列表查询")
    @PostMapping("ids")
    public JSONObject selectUserByIdsController(@RequestBody List<String> ids){
        List<User> users=userService.selectUserListByIds(ids);
        return Results.success(users,0);
    }

    @Autowired
    private CacheService cacheService;

    @ApiOperation("cache测试")
    @PostMapping("usercache")
    public JSONObject getUserByKey(String id){
        Object value=cacheService.getCache(id);
        User user=null;
        if(value==null){
            user=userService.getById(id);
            cacheService.putCache(id,user);
        }
        if(value!=null){
            user=(User)value;
        }
        return Results.success(user);
    }

    @ApiOperation("userList导出")
    @GetMapping("/import")
    public JSONObject importUserListController(HttpServletResponse response) throws Exception{
        List<User> userList=userService.list();
        List<UserModel> userModels=new ArrayList<>();
        for (int i = 0; i < userList.size() ; i++) {
            UserModel userModel=new UserModel();
            User user=userList.get(i);
            BeanUtils.copyProperties(user,userModel);
            userModels.add(userModel);
        }
        EasyExcelUtil.writeEasyExcel(response,UserModel.class,userModels,"用户列表");
        return Results.success();
    }

}
